package com.bw.zhuangao5___day002.Fragment.Movies;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvp.V.BannerFragment;
import com.bw.zhuangao5___day002.Fragment.Movies.One.Day3.JavaBeanDay3;
import com.bw.zhuangao5___day002.Fragment.Movies.One.Day3.MoviesDay3BaseAdpter;
import com.bw.zhuangao5___day002.Fragment.Movies.One.Day3.SQL.SQLoneday3;
import com.bw.zhuangao5___day002.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.ArrayList;
import java.util.List;


public class BlankFragmentMoviesGouWuChe extends BannerFragment implements OnItemChildClickListener {

    int qian = 0;
    List<JavaBeanDay3> getjava;
    private RecyclerView GouwucheView;
    private CheckBox GouwucheQuan;
    private CheckBox GouwucheFan;
    private TextView GouwucheZongjia;
    private Button GouwucheShan;
    private Button GouwucheZhifu;
    MoviesDay3BaseAdpter baseAdpter;
    private TextView GouwucheWored;


    @Override
    public int bandLayoud() {
        return R.layout.fragment_blank_fragment_movies_gouwuche;
    }

    @Override
    public void initView() {
        GouwucheView = (RecyclerView) findViewById(R.id.Gouwuche_View);
        GouwucheQuan = (CheckBox) findViewById(R.id.Gouwuche_Quan);
        GouwucheFan = (CheckBox) findViewById(R.id.Gouwuche_Fan);
        GouwucheZongjia = (TextView) findViewById(R.id.Gouwuche_Zongjia);
        GouwucheShan = (Button) findViewById(R.id.Gouwuche_Shan);
        GouwucheZhifu = (Button) findViewById(R.id.Gouwuche_Zhifu);
        GouwucheView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        GouwucheWored = (TextView) findViewById(R.id.Gouwuche_Wored);
    }

    @Override
    public void initDate() {

        GouwucheWored.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShort("刷新成功");
             shuaxin();
            }
        });

        SQLoneday3 sqLoneday3 = new SQLoneday3( getContext(),"quyang.db",null,1);
        SQLiteDatabase writableDatabase = sqLoneday3.getWritableDatabase();
        Cursor yang = writableDatabase.query("quyang", null, null, null, null, null, null);

        getjava = new ArrayList<>();
        while (yang.moveToNext())
        {
            String name = yang.getString(yang.getColumnIndex("name"));
            String pic = yang.getString(yang.getColumnIndex("pic"));
            String stringname = yang.getString(yang.getColumnIndex("stringname"));
            String cialiao = yang.getString(yang.getColumnIndex("cialiao"));
            int num = yang.getInt(yang.getColumnIndex("num"));
            JavaBeanDay3 javaBeanDay3 = new JavaBeanDay3(name,pic,stringname,cialiao,num);
            getjava.add(javaBeanDay3);
        }


        baseAdpter = new MoviesDay3BaseAdpter(getjava);
        baseAdpter.setOnItemChildClickListener(this);
        GouwucheView.setAdapter(baseAdpter);
        baseAdpter.notifyDataSetChanged();


        GouwucheShan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < getjava.size(); i++) {
                    if (getjava.get(i).isIschen()) {

                        SQLoneday3 sqLoneday3 = new SQLoneday3(getContext(), "quyang.db", null, 1);
                        SQLiteDatabase writableDatabase = sqLoneday3.getWritableDatabase();
                        writableDatabase.execSQL("delete from quyang where name=?", new Object[]{
                                getjava.get(i).getName()
                        });
                        getjava.remove(i).getName();
                        baseAdpter.notifyDataSetChanged();
                        break;
                    }else {
                        ToastUtils.showShort("请选择要删除的商品");
                    }

                }


            }
        });

        GouwucheZhifu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < getjava.size(); i++) {
                    if (getjava.get(i).isIschen()) {

                        break;

                    }

                }
            }
        });


        GouwucheQuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (JavaBeanDay3 javaBeanDay3 : getjava) {
                    if (GouwucheQuan.isChecked()) {
                        javaBeanDay3.setIschen(true);
                    } else {
                        javaBeanDay3.setIschen(false);
                    }

                }
                jisuannum();
                baseAdpter.notifyDataSetChanged();

            }
        });

        GouwucheFan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (JavaBeanDay3 javaBeanDay3 : getjava) {
                    if (javaBeanDay3.isIschen()) {
                        javaBeanDay3.setIschen(false);
                    } else {
                        javaBeanDay3.setIschen(true);
                    }

                }
                jisuannum();
                baseAdpter.notifyDataSetChanged();

            }
        });


    }

    public void jisuannum() {
        int dahui =0;
        for (int i = 0; i < getjava.size(); i++) {
            if (getjava.get(i).isIschen()) {
                int aidasi = getjava.get(i).getNum() * getjava.get(i).noosumer;
                dahui += aidasi;
            }

        }

        GouwucheZongjia.setText("总价格是：" + dahui);
        qian = dahui;
    }

    public void quanqu() {
        int quxiao = 0;
        for (int i = 0; i < getjava.size(); i++) {
            if (!getjava.get(i).isIschen()) {
                quxiao = 1;
                break;
            }
        }

        if (quxiao == 0) {
            GouwucheQuan.setChecked(true);
        } else {
            GouwucheQuan.setChecked(false);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.Tianzi_Checkbox:
                getjava.get(position).setIschen(!getjava.get(position).ischen);
                quanqu();
                jisuannum();
                baseAdpter.notifyDataSetChanged();
                break;

            case R.id.Tianzi_Button1:
                if (getjava.get(position).noosumer == 1) {
                    ToastUtils.showShort("不能在减了");
                } else {
                    getjava.get(position).noosumer--;
                }
                jisuannum();
                baseAdpter.notifyDataSetChanged();
                break;

            case R.id.Tianzi_Button2:
                getjava.get(position).noosumer++;
                jisuannum();
                baseAdpter.notifyDataSetChanged();
                break;
        }
    }


    public void shuaxin() {
        SQLoneday3 sqLoneday3 = new SQLoneday3(getContext(), "quyang.db", null, 1);
        SQLiteDatabase writableDatabase = sqLoneday3.getWritableDatabase();
        Cursor yang = writableDatabase.query("quyang", null, null, null, null, null, null);

        getjava = new ArrayList<>();
        while (yang.moveToNext()) {
            String name = yang.getString(yang.getColumnIndex("name"));
            String pic = yang.getString(yang.getColumnIndex("pic"));
            String stringname = yang.getString(yang.getColumnIndex("stringname"));
            String cialiao = yang.getString(yang.getColumnIndex("cialiao"));
            int num = yang.getInt(yang.getColumnIndex("num"));
            JavaBeanDay3 javaBeanDay3 = new JavaBeanDay3(name, pic, stringname, cialiao, num);
            getjava.add(javaBeanDay3);

        }
        baseAdpter = new MoviesDay3BaseAdpter(getjava);
        baseAdpter.setOnItemChildClickListener(this);
        GouwucheView.setAdapter(baseAdpter);
        baseAdpter.notifyDataSetChanged();

    }
}
